//
//  KKAllModuleDialog.m
//  DoMi
//
//  Created by 康凯 on 2016/11/23.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKAllModuleDialog.h"
#import "KKItemCell.h"
#import "KKStudyItem.h"
#import "KKItemListController.h"

@interface KKAllModuleDialog()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UIView *bgShadow;
@property (weak, nonatomic) IBOutlet UIView *container;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation KKAllModuleDialog

-(void)awakeFromNib{
    [super awakeFromNib];
    self.container.autoresizingMask =UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.container.translatesAutoresizingMaskIntoConstraints = YES;
    [self initData];
}

-(void)initData{
    self.frame = [UIScreen mainScreen].bounds;
    self.container.frame=CGRectMake(0, screenHeight, screenWidth, 400);
    self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [self.bgShadow addGestureRecognizer:tap];
    
    //确定是水平滚动，还是垂直滚动
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    flowLayout.itemSize=CGSizeMake((screenWidth-20)/2, 40);
    flowLayout.minimumLineSpacing=1;
    flowLayout.minimumInteritemSpacing=1;
    
    self.collectionView.collectionViewLayout=flowLayout;
    self.collectionView.dataSource=self;
    self.collectionView.delegate=self;
    self.collectionView.backgroundColor=[UIColor colorWithHexString:@"#E3E3E0"];
    self.collectionView.layer.cornerRadius=4;
    
    //注册Cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"KKItemCell" bundle:nil] forCellWithReuseIdentifier:@"KKItemCell"];
}



#pragma mark collectionview代理
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.items.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    KKItemCell *cell = (KKItemCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"KKItemCell" forIndexPath:indexPath];
    KKStudyItem *item=self.items[indexPath.row];
    [cell initData:item.title];
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((screenWidth-19)/2, 40);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if(section==0){
        return UIEdgeInsetsMake(4, 4, 0, 4);
    }
    return UIEdgeInsetsMake(0, 4, 0, 4);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    KKStudyItem *item= self.items[indexPath.row];
    KKItemListController *itemView=[[KKItemListController alloc] init];
    itemView.codeId=item.code;
    itemView.barTitle=item.title;

    [self.studyVC.navigationController pushViewController:itemView animated:YES];
//    [[KKAppUtils getCurrentVisiableVC:self].navigationController pushViewController:itemView animated:YES];
    [self dismiss];
}

#pragma mark 按钮--取消
- (IBAction)cancelBtn:(id)sender {
    [self dismiss];
}
#pragma makr 弹框--展示
-(void)show{
    [appWindow addSubview:self];
    [UIView animateWithDuration:dialog_animation_time animations:^{
        self.container.transform = CGAffineTransformMakeTranslation(0,-400);
    }];
//    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:5 options:UIViewAnimationOptionCurveEaseOut animations:^{
//        
//    } completion:^(BOOL finished) {
//    }];
}
#pragma makr 弹框--消失
-(void)dismiss{
    [UIView animateWithDuration:dialog_animation_time animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        self.container.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
